from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required
from model import Course, Teacher, StudentCourse, TeacherCourse  # Ensure you import your models
from extension import db  # Assuming you're using Flask-SQLAlchemy
from views.user import admin_required

teacher_bp = Blueprint('teacher', __name__)


@teacher_bp.route('getinfo/<teacher_id>', methods=['GET'])
@jwt_required()
@admin_required
def get_teacher_info(teacher_id):
    # Retrieve teacher information from the database
    teacher = Teacher.query.filter_by(teacher_id=teacher_id).first()

    if teacher:
        teacher_info = {
            'teacher_id': teacher.teacher_id,
            'name': teacher.name,
            #'email': teacher.email,  # Include any other necessary fields
            # Avoid returning sensitive information like password
        }
        return jsonify(teacher_info), 200
    else:
        return jsonify({'message': 'Teacher not found.'}), 404


@teacher_bp.route('getcourse/<teacher_id>', methods=['GET'])
@jwt_required()
@admin_required
def get_teacher_courses(teacher_id):
    # Retrieve courses taught by the specified teacher
    teacher = Teacher.query.filter_by(teacher_id=teacher_id).first()
    teacherCourse = TeacherCourse.query.filter_by(teacher_id=teacher_id).all()

    if not teacherCourse:
        return jsonify([]), 200  # Return an empty list if no courses found

    course_list = []
    for teachercourse in teacherCourse:
        course = Course.query.filter_by(course_id=teachercourse.course_id).first()
        course_list.append({
            'course_id': course.course_id,
            'course_sequence': course.course_sequence,
            'course_name': course.course_name,
            'teacher_id': teacher.teacher_id,
            'teacher_name': teacher.name
            # Add any other relevant fields from the Course model
        })

    return jsonify(course_list), 200
